import { Badge, Descriptions, Drawer } from "antd";
import { forwardRef, useImperativeHandle, useMemo, useState } from "react"
import DisplayLabel from "@/components/displayLabel";

const LogViewFormModal = forwardRef((props, ref) => {
  const [fullData, setFullData] = useState({
    jobId: undefined,
    jobName: undefined,
    jobGroup: undefined,
    invokeTarget: undefined,
    cronExpression: undefined,
    misfirePolicy: '1',
    concurrent: '1',
    status: '0'
  })
  const [isModalOpen, setIsModalOpen] = useState()

  const borderedItems = useMemo(() => {
    let r = [
      {
        key: 'jobLogId',
        label: '日志序号',
        children: fullData.jobLogId,
      },
      {
        key: 'jobName',
        label: '任务名称',
        children: fullData.jobName,
      },
      {
        key: 'jobGroup',
        label: '任务分组',
        children: <DisplayLabel value={fullData.jobGroup} type={'sys_job_group'} />,
      },
      {
        key: 'status',
        label: '执行状态',
        children: <Badge status={fullData.status == '0' ? 'processing' : 'warning'} text={<DisplayLabel value={fullData.status} type="sys_job_status" />} />,
      },
      {
        key: 'createTime',
        label: '执行时间',
        children: fullData.createTime,
        span: 2,
      },
      {
        key: 'invokeTarget',
        label: '调用方法',
        children: fullData.invokeTarget,
        span: 3,
      },
      {
        key: 'jobMessage',
        label: '日志信息',
        children: fullData.jobMessage,
        span: 3,
      },
    ]
    if (fullData.status == 1) {
      r.push({
        key: 'exceptionInfo',
        label: '异常信息',
        children: fullData.exceptionInfo,
        span: 3,
      })
    }
    return r
  }, [fullData])

  useImperativeHandle(ref, () => ({
    showModal: (data) => {
      setFullData(data)
      setIsModalOpen(true)
    },
  }));

  const handleCancel = () => {
    setIsModalOpen(false)
  };
  return <Drawer width={750} open={isModalOpen} placement="right"
    closable={false} onClose={handleCancel}>
    <Descriptions
      title="调度日志详细"
      layout="vertical"
      bordered
      column={3}
      items={borderedItems}
    />
  </Drawer>
})

export default LogViewFormModal